package com.frxuexi.park2304.news

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.SearchView.OnQueryTextListener
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.ItemTouchHelper.LEFT
import androidx.recyclerview.widget.ItemTouchHelper.SimpleCallback
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.NewsBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.park2304.R
import com.frxuexi.park2304.databinding.ActivityNewsBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/NewsActivity")
@AndroidEntryPoint
class NewsActivity : BaseActivity<ActivityNewsBinding,NewsIntent,NewsViewModel>() {
    override val binding: ActivityNewsBinding by lazy { ActivityNewsBinding.inflate(layoutInflater) }
    override val vm: NewsViewModel by viewModels<NewsViewModel>()
    val newsAdapter by lazy { NewsAdapter() }
    val list= mutableListOf<NewsBeanItem>()

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                val items = it.data as Collection<NewsBeanItem>
                                list.clear()
                                list.addAll(items.filter { it.published })
                                newsAdapter.data.clear()
                                newsAdapter.addData(list)
                                newsAdapter.notifyDataSetChanged()
                            }
                            RequestType.UPDATE->{
                                ToastUtils.showLong("撤销完成")
                                vm.sendIntent(NewsIntent.getNews)
                            }
                            RequestType.ITEM->{
                                val items = it.data as Collection<NewsBeanItem>
                                newsAdapter.data.clear()
                                newsAdapter.addData(items.filter { it.published })
                                newsAdapter.notifyDataSetChanged()
                            }

                            RequestType.DELETE->{
                                vm.sendIntent(NewsIntent.getNews)
                            }

                            else -> {}
                        }

                    }


                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.newsRv.apply {
            adapter=newsAdapter
            layoutManager=LinearLayoutManager(this@NewsActivity)
        }

        newsAdapter.addChildClickViewIds(R.id.new_detail)
        newsAdapter.setOnItemChildClickListener { adapter, view, position ->
            vm.sendIntent(NewsIntent.revokeNews(newsAdapter.data.get(position).id!!))
        }

        binding.newsSv.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                vm.sendIntent(NewsIntent.getNewsByCon(query!!))
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                newsAdapter.data.clear()
                newsAdapter.addData(list)
                newsAdapter.notifyDataSetChanged()
                return true
            }
        })

        binding.newsAdd.setOnClickListener {
            ArouterManager.nav(this,"/app/NesAddActivity")
        }

        ItemTouchHelper(object : SimpleCallback(LEFT, LEFT) {
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.adapterPosition
                vm.sendIntent(NewsIntent.deleteNews(newsAdapter.data.get(position).id!!))
            }
        }).attachToRecyclerView(binding.newsRv)

        newsAdapter.setOnItemClickListener { adapter, view, position ->
            ArouterManager.nav(this,"/app/NewsUpdateActivity",newsAdapter.data.get(position).id!!)
        }
    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(NewsIntent.getNews)
    }

}